package com.xqboss.apps.mapper.member;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xqboss.apps.domain.member.SysUserNft;
import com.xqboss.apps.dto.user.UserNftListDto;
import com.xqboss.apps.vo.punch.UserPunchEquipmentVo;
import com.xqboss.apps.vo.sys.SnapshotVO;
import com.xqboss.apps.vo.user.UserNFTItemVo;
import com.xqboss.apps.vo.user.UserNFTVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户NFT表 Mapper 接口
 * </p>
 *
 * @author lang.liu
 * @since 2022-6-2
 */
public interface SysUserNftMapper extends BaseMapper<SysUserNft> {

    /**
     * 获取商品流通总量
     * @return
     */
    Integer getNftCirculate(Long  nftId);

    /**
     * 获取用户持有总数（有多少个用户拥有）
     * @param id
     * @return
     */
    @Select("select count(1) from (select count(1) from sys_member_nft where nft_id = #{id} and revenue_expen_type in(0,2,3,4,6,7,11)  and delete_time is null GROUP BY user_id) sys_member_nft")
    Integer getNumberOfHolders(Long id);

    /**
     * 藏品在售数量
     * @param id
     * @return
     */
    @Select("select count(1) from sys_member_nft where nft_id = #{id} and revenue_expen_type in(3,4) and delete_time is null")
    Integer getSalesNum(Long id);


    UserNFTItemVo getNftInfoById(@Param("id") Long id, @Param("userId") Long userId);

    /**
     * 获取以藏品为单位的藏品列表
     * @param params
     * @return
     */
    List<UserNFTVo> getNftGroupList(UserNftListDto params);

    /**
     * 获取以藏品编号为单位的藏品列表
     * @param params
     * @return
     */
    List<UserNFTItemVo> getNftNumberList(Object params);

    List<SnapshotVO> selectSnapshot(@Param("nftId") Long nftId, @Param("userId") Long userId);

    /**
     * 重置打卡规则的装备状态
     * @param punchRuleId
     */
    long resetNftStatusByPunchRuleId(Long punchRuleId);

    List<SnapshotVO> selectSnapshotBox(@Param("boxId") Long boxId, @Param("userId") Long userId);

    /**
     * 统计满足条件的藏品数量
     * @param userNftIdList
     * @param levelThreshold
     * @param breakageThreshold
     * @param durableThreshold
     * @return
     */
    int countEquipment(@Param("userId") Long userId,
                       @Param("nftId") Long nftId,
                       @Param("userNftIdList") List<Long> userNftIdList,
                       @Param("levelThreshold") Integer levelThreshold,
                       @Param("breakageThreshold") BigDecimal breakageThreshold,
                       @Param("durableThreshold") BigDecimal durableThreshold,
                       @Param("minDurable") BigDecimal minDurable,
                       @Param("maxDurable") BigDecimal maxDurable,
                       @Param("minBreakage") BigDecimal minBreakage,
                       @Param("maxBreakage") BigDecimal maxBreakage,
                       @Param("minLevel") Integer minLevel,
                       @Param("maxLevel") Integer maxLevel,
                       @Param("createTime") LocalDateTime createTime);

    @Update("update sys_user_nft set nft_status = #{nftStatus} , del_flag = true where user_id = #{userId} and nft_number_id = #{nftNumberId} and del_flag=false and nft_status=0")
    boolean identityPromotionDestruction(@Param("userId") Long userId,
                                      @Param("nftNumberId") Long nftNumberId,
                                      @Param("nftStatus") Integer value);

    /**
     * 获取商品类型为箱子的列表
     *
     * @param params params
     * @return List
     */
    List<UserNFTItemVo> getBoxNftNumberList(UserNftListDto params);

    /**
     * 获取装备列表
     * @return 装备
     */
    List<UserPunchEquipmentVo> equipmentList(@Param("userId") Long userId, @Param("nftStatus") Integer nftStatus);
}
